var op_tabbed_content = (function(){
	return{
		// build an array of all of the tabs on the page to setup event listeners
		init: function(){
			var elements, // object of all <div>'s in document
				tabs = [], // array to push results into
				h, i, // for loops
				elems; // object of all <li>'s in <div>
				
			elements = document.getElementsByTagName("div");
			tabs = [];
		    for(i = 0; i < elements.length;i++){
	            if(elements[i].className == 'op_tabbed_tabs'){
	                    tabs.push(elements[i]);
	            }
	        }
			for(h=0; h < tabs.length; h++){
				elems = tabs[h].getElementsByTagName('li');
				for(i=0; i < elems.length; i++){
					OP.utility.observe(elems[i],'click',op_tabbed_content.switchTab);
				}
			}
		},
		
		switchTab: function(){
			var tmp, 		// store event object
				newId, 		// id for even trigger object
				matched, 	// matched parentNodes
				widget_id,
				elems, 		// store widget <div>'s
				i, j,		// for loops
				thisId, 	// current id
				classbase, 	// classbase to assign
				tab_elems; 	// store widget <li>'s

			// get event target.  this won't have an id in IE
			tmp = this.id == undefined ? event.srcElement : this;
			newId = tmp.className.split('-')[1];
			
			// Handling whitespace trick taken from jQuery
			matched = [], current = tmp['parentNode'];
			while ( current && current != document ) {
				if ( current.nodeType == 1 )
					matched.push( current );
				current = current['parentNode'];
			}
			tmp = matched; // tmp is all parentNodes w/o whitespace

			// get widget_id from event target element
			widget_id = tmp[1].id.split('-')[1];

			// Handle datasets.  Show active by hiding all not selected with css class "hide"
			elems = document.getElementById('op_tabbed_datasets-'+widget_id).getElementsByTagName('div');
			for(i=0; i < elems.length; i++){
				thisId = elems[i].className.split(' ')[0].split('-')[1]; // get id dataset[i]
				if(isNaN(thisId)) continue; // if thisId is not a number, skip class manipulation
				classbase = 'tabbed_dataset-'+thisId; // build class to be assigned
				elems[i].className = thisId == newId ? classbase : classbase+ ' hide'; // hide datasets if ids don't match
			}
			
			// Handle tabs.  Show selected tab by adding css class "selected"
			tab_elems = document.getElementById('op_tabbed_tabs-'+widget_id).getElementsByTagName('li');
			for(j=0; j < tab_elems.length; j++){
				thisId = tab_elems[j].className.split(' ')[0].split('-')[1]; // get id of tab[j]
				classbase = 'tabbed_tab-'+thisId; // build class to be assigned
				tab_elems[j].className = thisId != newId ? classbase : classbase+ ' selected';  // append selected to class if id matches
			}
		}
	}
})();

OP.utility.addLoadProcess(op_tabbed_content.init);